﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class YP_LJTZCZD : DevComponents.DotNetBar.Office2007Form
    {
        public YP_LJTZCZD()
        {
            InitializeComponent();
        }
        SqlDataAdapter da = new SqlDataAdapter();//实例化sqldataadpte
        DataTable dt = new DataTable();
        DataTable dt_czd = new DataTable();
        int insert_switch,saved;
        string cmd_ZSG = "BEGIN TRANSACTION ";

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if ((ActiveControl is TextBox || ActiveControl is Button) && keyData == Keys.Enter)
                keyData = Keys.Tab;
            return base.ProcessDialogKey(keyData);
        }

        private void data_view()
        {
            dataGridViewX1.Columns[0].Visible = false;
            dataGridViewX1.Columns[1].HeaderText = "药品编码";
            dataGridViewX1.Columns[2].HeaderText = "药品名称";
            dataGridViewX1.Columns[3].HeaderText = "原价";
            dataGridViewX1.Columns[4].HeaderText = "现价";
            dataGridViewX1.Columns[5].HeaderText = "差价";
            dataGridViewX1.Columns[6].HeaderText = "库存数量";
            dataGridViewX1.Columns[7].HeaderText = "损益金额";
            dataGridViewX1.Columns[8].Visible = false;
        }

        private void data_refresh()
        {
            using (SqlConnection link = new SqlConnection(SQL.connectDB))
            {
                SqlCommand cmd2 = new SqlCommand("SELECT * FROM YP_LJTZCZD WHERE TJDH='" + txt_DH.Text.ToString() + "'", link);
                da.SelectCommand = cmd2;
                da.Fill(dt_czd);
                dataGridViewX1.DataSource = dt_czd;
            }
        }

        private void font_color(TextBox txt)
        {
            if (txt.Text.Substring(0, 1) == "-")
                txt.ForeColor = System.Drawing.Color.DarkRed;
            else
                txt.ForeColor = System.Drawing.Color.DarkGreen;
        }

        private void cell_color()
        {
            for (int i = 0; i < dataGridViewX1.RowCount; i++)
            {
                if (Convert.ToInt32(dataGridViewX1.Rows[i].Cells[7].Value) > 0)
                    dataGridViewX1.Rows[i].Cells[7].Style.ForeColor = System.Drawing.Color.Green;
                else dataGridViewX1.Rows[i].Cells[7].Style.ForeColor = System.Drawing.Color.Red;
            }
        }

        private void YP_LJTZCZD_Load(object sender, EventArgs e)
        {
            dateTimeInput1.Value = DateTime.Today;
            txt_DH.Text = DateTime.Today.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2,'0')+ DateTime.Now.Day.ToString().PadLeft(2,'0') + DateTime.Now.Hour.ToString().PadLeft(2,'0') + DateTime.Now.Minute.ToString().PadLeft(2,'0') + DateTime.Now.Second.ToString().PadLeft(2,'0');
            try
            {
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {

                    SqlCommand sc = con.CreateCommand();//使用connection对象创建SqlCommand对象
                    SqlCommand cmd1 = new SqlCommand("SELECT BM,DH,MC,DJ FROM ZD_YP", con);//sql语句
                    da.SelectCommand = cmd1;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt);
                    dataGridViewX2.DataSource = dt;//药品字典
                    if (YP_LJTZCZ.currow != null)
                    {
                        txt_DH.Text = YP_LJTZCZ.currow.ToString();
                        cmb_TJLX.Text=YP_LJTZCZ.str_tjlx.ToString();
                        cmb_TJLX.Enabled = false;
                        txt_PWMC.Text=YP_LJTZCZ.str_pwmc.ToString();
                        txt_PWMC.ReadOnly = true;
                        txt_PWDW.Text=YP_LJTZCZ.str_pwdw.ToString();
                        txt_PWDW.ReadOnly = true;
                        txt_BZ.Text=YP_LJTZCZ.str_bz.ToString();
                        txt_BZ.ReadOnly = true;
                        dateTimeInput1.Value = Convert.ToDateTime(YP_LJTZCZ.time_ZDRQ);
                        dateTimeInput1.Enabled = false;
                        dateTimeInput2.Value = Convert.ToDateTime(YP_LJTZCZ.time_PWRQ);
                        dateTimeInput2.Enabled = false;
                        data_refresh();
                        insert_switch = 0;
                    }
                    else
                    {
                        txt_DH.Text = DateTime.Today.Year.ToString() + DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0') + DateTime.Now.Hour.ToString().PadLeft(2, '0') + DateTime.Now.Minute.ToString().PadLeft(2, '0') + DateTime.Now.Second.ToString().PadLeft(2, '0');
                        insert_switch = 1;
                        data_refresh();
                    }
                    data_view();
                    cell_color();
                    
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void save()
        {
            if (YP_LJTZCZ.state_flag == 2)
                MessageBox.Show("该冲账单号已记账,不能更改!", "提示");
            else if (YP_LJTZCZ.state_flag == 1)
                MessageBox.Show("该冲账单号已审核,请先消审!", "提示");
            else
            {
                cmd_ZSG = cmd_ZSG + " COMMIT TRANSACTION ";

                try
                {
                    SqlConnection cn = new SqlConnection(SQL.connectDB);

                    {
                        cn.Open();

                        SqlCommand com = new SqlCommand(cmd_ZSG, cn);

                        com.ExecuteNonQuery();

                        if (cmd_ZSG != "BEGIN TRANSACTION  COMMIT TRANSACTION ")
                        {
                            MessageBox.Show("保存成功", "提示");

                            saved = 1;
                        }

                        cmd_ZSG = "BEGIN TRANSACTION ";
                        
                    }
                }
                catch { }
            }
        }

        #region 保存
        private void buttonItem1_Click(object sender, EventArgs e)
        {
            save();
        
        }
        #endregion 



        private void dataGridViewX1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
           
        }

        private void dataGridViewX1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
           
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Visible = false;
            label2.Visible = false;
        }


        #region 删除
        private void buttonX3_Click(object sender, EventArgs e)
        {
            try
            {
                cmd_ZSG = cmd_ZSG + " DELETE FROM YP_LJTZCZD WHERE TJDH='"+txt_DH.Text.ToString()+"' AND YPBM='" + dataGridViewX1.CurrentRow.Cells[1].Value.ToString() + "'";

                dataGridViewX1.Rows.Remove(dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex]);

            }
            catch { }
        }
        #endregion

        private void dataGridViewX1_MouseClick(object sender, MouseEventArgs e)
        {
            txt_BM.Text = dataGridViewX1.CurrentRow.Cells[1].Value.ToString();
            dataGridViewX2.Visible = false;
            txt_MC.Text = dataGridViewX1.CurrentRow.Cells[2].Value.ToString();
            txt_YJ.Text = dataGridViewX1.CurrentRow.Cells[3].Value.ToString();
            txt_XJ.Text = dataGridViewX1.CurrentRow.Cells[4].Value.ToString();
            txt_CJ.Text = dataGridViewX1.CurrentRow.Cells[5].Value.ToString();
            txt_KCSL.Text = dataGridViewX1.CurrentRow.Cells[6].Value.ToString();
            txt_SYJE.Text = dataGridViewX1.CurrentRow.Cells[7].Value.ToString();
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            txt_BM.Text = "";
            dataGridViewX2.Visible = false;
            txt_MC.Text = "";
            txt_YJ.Text = "";
            txt_XJ.Text = "";
            txt_CJ.Text = "";
            txt_KCSL.Text = "";
            txt_SYJE.Text = "";
        }

        private void panel2_Click(object sender, EventArgs e)
        {
            dataGridViewX2.Visible = false;
        }

        private void groupBox2_MouseCaptureChanged(object sender, EventArgs e)
        {
            dataGridViewX2.Visible = false;

        }

        private void YP_LJTZCZD_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (cmd_ZSG != "BEGIN TRANSACTION " && YP_LJTZCZ.state_flag == 0 && saved!=1)
            {
                if (MessageBox.Show("数据还未保存，是否保存 ？", "提示...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    save();
                    
                }
                else
                {
                    e.Cancel = false;
                }
            }
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            dgVprint1.Print(dataGridViewX1, false);
        }
        #region 确定
        private void btn_QD_Click_1(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    con.Open();
                    SqlCommand sc = new SqlCommand("SELECT COUNT(*) FROM YP_LJTZCZD WHERE TJDH='" + txt_DH.Text.ToString() + "' AND YPBM='" + txt_BM.Text.ToString() + "'", con);
                    da.SelectCommand = sc;
                    // sc.ExecuteScalar()
                    if (Convert.ToInt16(sc.ExecuteScalar()) > 0)
                    {
                        cmd_ZSG = cmd_ZSG + " UPDATE YP_LJTZCZD SET XDJ='" + txt_XJ.Text.ToString() + "',SYDJ='" + txt_CJ.Text.ToString() + "',KCSL='" + txt_KCSL.Text.ToString() + "',SYJE='"
                                  + txt_SYJE.Text.ToString() + "',FLAG=0";

                        for (int i = 0; i < dataGridViewX1.RowCount - 1; i++)
                            if (dataGridViewX1.Rows[i].Cells[1].Value.ToString() == txt_BM.Text.ToString())
                            {
                                dataGridViewX1.Rows[i].Cells[4].Value = txt_XJ.Text.ToString();
                                dataGridViewX1.Rows[i].Cells[5].Value = txt_CJ.Text.ToString();
                                dataGridViewX1.Rows[i].Cells[6].Value = txt_KCSL.Text.ToString();
                                dataGridViewX1.Rows[i].Cells[7].Value = txt_SYJE.Text.ToString();
                                cell_color();

                            }

                    }

                    else
                    {
                        if (insert_switch == 1)
                        {
                            if (dateTimeInput1.Value.ToString() == "0001-1-1 0:00:00")
                                MessageBox.Show("请输入制单日期", "提示");
                            else if (dateTimeInput2.Value.ToString() == "0001-1-1 0:00:00")
                                MessageBox.Show("请输入批文日期", "提示");
                            else
                            {

                                string[] strArray ={ txt_DH.Text.ToString(), txt_BM.Text.ToString(), txt_MC.Text.ToString(), txt_YJ.Text.ToString(), txt_XJ.Text.ToString(), txt_CJ.Text.ToString(), txt_KCSL.Text.ToString(), txt_SYJE.Text.ToString(), "0" };
                                ((DataTable)dataGridViewX1.DataSource).Rows.Add(strArray);

                                //插入成功能才往YP_LJTZCZ里insert;

                                cmd_ZSG = cmd_ZSG + " INSERT INTO YP_LJTZCZD(TJDH,YPBM,YPMC,YDJ,XDJ,SYDJ ,KCSL,SYJE,FLAG) VALUES('" + txt_DH.Text.ToString() + "','" + txt_BM.Text.ToString() + "','" + txt_MC.Text.ToString() + "','"
                                    + txt_YJ.Text.ToString() + "','" + txt_XJ.Text.ToString() + "','" + txt_CJ.Text.ToString() + "','" + txt_KCSL.Text.ToString() + "','" + txt_SYJE.Text.ToString() + "',0)";


                                cmd_ZSG = cmd_ZSG + "  INSERT INTO YP_LJTZCZ(TJDH,RQ,LRRY,FLAG,BZ,TJLX,PWMC,PWDW,PWRQ) VALUES ('" + txt_DH.Text.ToString() + "','" + dateTimeInput1.Value + "','" + LoginForm.username + "','0','" + txt_BZ.Text.ToString() + "','" + cmb_TJLX.Text.ToString() + "','" + txt_PWMC.Text.ToString() + "','" + txt_PWDW.Text.ToString() + "','" + dateTimeInput2.Value + "')";



                                cell_color();

                                insert_switch = 0;
                            }
                        }

                    }
                    //改字典药品表里面的DJ1，记账时单价=DJ1
                    string sqlgj = "UPDATE ZD_YP SET DJ1='" + txt_XJ.Text.ToString() + "' WHERE BM ='" + txt_BM.Text.ToString() + "'";
                    SqlCommand gj = new SqlCommand(sqlgj, con);
                    gj.ExecuteNonQuery();
                }
            }
            catch 
            {
               
            }

        }
        #endregion
        private void txt_BM_TextChanged(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(SQL.connectDB))
            {
                DataTable dt_bmdw = new DataTable();
                SqlCommand BMDW = new SqlCommand("SELECT ZD_YP.BM 编码,MC 名称,DJ,DH,KCSL FROM ZD_YP LEFT JOIN (select YPBM ,SUM(SL) AS KCSL from YP_CRKJZ group by YPBM) YPKC ON ZD_YP.BM=YPBM WHERE BM LIKE'%" + txt_BM.Text.ToString() + "%' OR DH LIKE'%" + txt_BM.Text.ToString() + "%' OR MC LIKE'%" + txt_BM.Text.ToString() + "%'", con);//sql语句
                da.SelectCommand = BMDW;//设置为已实例化SqlDataAdapter的查询命令
                da.Fill(dt_bmdw);
                dataGridViewX2.DataSource = dt_bmdw;
                dataGridViewX2.Columns[0].Width = 80;
                dataGridViewX2.Columns[1].Width = 200;
                dataGridViewX2.Columns[2].Visible = false;
                dataGridViewX2.Columns[3].Visible = false;
                dataGridViewX2.Columns[4].Visible = false;

                dataGridViewX2.Size = new Size(dataGridViewX2.Columns[0].Width + dataGridViewX2.Columns[1].Width + 28, 158);
                dataGridViewX2.Visible = true;

            }
        }

        private void txt_XJ_TextChanged_1(object sender, EventArgs e)
        {
            txt_CJ.Text = "";
            txt_SYJE.Text = "";
            Double XJ=0;
            Double KCSL=0;

            if (txt_XJ.Text != "")
            {
                try
                {
                     XJ = Convert.ToDouble(txt_XJ.Text);
                }

                catch
                {

                    timer1.Enabled = true;
                    txt_XJ.Text = "";
                    txt_CJ.Text = "";
                    label1.Text = "格式不正确";
                    txt_XJ.Focus();
                    label1.Visible = true;
                }
            }
            if (txt_KCSL.Text != "")
            {
                try
                {
                     KCSL = Convert.ToDouble(txt_KCSL.Text);
                }

                catch
                {
                    timer1.Enabled = true;
                    txt_KCSL.Text = "";
                    txt_SYJE.Text = "";
                    label2.Text = "格式不正确";
                    txt_KCSL.Focus();
                    label2.Visible = true;
                }
            }
            txt_CJ.Text = Convert.ToString(XJ - Convert.ToDouble(txt_YJ.Text));
            txt_SYJE.Text = Convert.ToString(Convert.ToDouble(txt_CJ.Text) * KCSL);
            font_color(txt_CJ);
            font_color(txt_SYJE);
        }

        private void dataGridViewX2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txt_BM.Text = dataGridViewX2.Rows[dataGridViewX2.CurrentCell.RowIndex].Cells[0].Value.ToString();
            txt_MC.Text = dataGridViewX2.Rows[dataGridViewX2.CurrentCell.RowIndex].Cells[1].Value.ToString();
            txt_YJ.Text = dataGridViewX2.Rows[dataGridViewX2.CurrentCell.RowIndex].Cells[2].Value.ToString();
            txt_KCSL.Text = dataGridViewX2.Rows[dataGridViewX2.CurrentCell.RowIndex].Cells[4].Value.ToString();
            if (txt_KCSL.Text.ToString() == "")
                txt_KCSL.Focus();
            else txt_XJ.Focus();
            dataGridViewX2.Visible = false;
        }
    }
}